//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "MarketModel.h"
using namespace Cephei::QL::Models::Marketmodels;
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::CMarketModel::CMarketModel (boost::shared_ptr<QuantLib::MarketModel>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModel = NULL;
#endif
	_ppMarketModel = &childNative;
    
}
Cephei::QL::Models::Marketmodels::CMarketModel::CMarketModel (QuantLib::MarketModel& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phMarketModel = NULL;
#endif
	_ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (&childNative);
    
    _MarketModelOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::CMarketModel::CMarketModel (CMarketModel^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::CMarketModel::CMarketModel (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModel = NULL;
#endif
	if (!t->IsSubclassOf(CMarketModel::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::CMarketModel::CMarketModel (QuantLib::Handle<QuantLib::MarketModel>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMarketModel = &childNative;
	_ppMarketModel = &static_cast<boost::shared_ptr<QuantLib::MarketModel>>(childNative.currentLink());
    
    _MarketModelOwner = owner;
}
Cephei::QL::Models::Marketmodels::CMarketModel::CMarketModel (QuantLib::Handle<QuantLib::MarketModel> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMarketModel = &childNative;
	_ppMarketModel = &static_cast<boost::shared_ptr<QuantLib::MarketModel>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::CMarketModel::CMarketModel (QuantLib::MarketModel childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModel = NULL;
#endif
	_ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (new QuantLib::MarketModel (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::CMarketModel::~CMarketModel ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppMarketModel != NULL)
    {
	    delete _ppMarketModel;
        _ppMarketModel = NULL;
    }
}
Cephei::QL::Models::Marketmodels::CMarketModel::!CMarketModel ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppMarketModel != NULL)
    {
	    delete _ppMarketModel;
    }
}
QuantLib::MarketModel& Cephei::QL::Models::Marketmodels::CMarketModel::GetReference ()
{
    if (_ppMarketModel == NULL) throw REFNEW NativeNullException ();
	return **_ppMarketModel;
}
boost::shared_ptr<QuantLib::MarketModel>& Cephei::QL::Models::Marketmodels::CMarketModel::GetShared ()
{
    if (_ppMarketModel == NULL) throw REFNEW NativeNullException ();
	return *_ppMarketModel;
}
QuantLib::MarketModel* Cephei::QL::Models::Marketmodels::CMarketModel::GetPointer ()
{
    if (_ppMarketModel == NULL) throw REFNEW NativeNullException ();
	return &**_ppMarketModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MarketModel>& Cephei::QL::Models::Marketmodels::CMarketModel::GetHandle ()
{
	if (_phMarketModel == NULL)
	{
		_phMarketModel = new Handle<QuantLib::MarketModel> (*_ppMarketModel);
	}
	return *_phMarketModel;
}
#endif
bool Cephei::QL::Models::Marketmodels::CMarketModel::HasNative () 
{
	return (_ppMarketModel != NULL);
}

Cephei::QL::Math::IMatrix^ Cephei::QL::Models::Marketmodels::CMarketModel::Covariance (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppMarketModel)->covariance ( _i );   
        Cephei::QL::Math::CMatrix^ _nrv = REFNEW Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::CMarketModel::Displacements::get ()
{
    try
    {
    	std::vector<QuantLib::Spread>& _rv = (std::vector<QuantLib::Spread>&)(*_ppMarketModel)->displacements ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::CMarketModel::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppMarketModel)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::CMarketModel::InitialRates::get ()
{
    try
    {
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppMarketModel)->initialRates ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::CMarketModel::NumberOfFactors::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModel)->numberOfFactors ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::CMarketModel::NumberOfRates::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModel)->numberOfRates ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::CMarketModel::NumberOfSteps::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModel)->numberOfSteps ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Models::Marketmodels::CMarketModel::PseudoRoot (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppMarketModel)->pseudoRoot ( _i );   
        Cephei::QL::Math::CMatrix^ _nrv = REFNEW Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::CMarketModel::TimeDependentVolatility (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	std::vector<QuantLib::Volatility> _rv = (std::vector<QuantLib::Volatility>)(*_ppMarketModel)->timeDependentVolatility ( _i );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Models::Marketmodels::CMarketModel::TotalCovariance (UInt64 endIndex)
{
    try
    {
        QuantLib::Size _endIndex = (QuantLib::Size)ValueHelper::Convert (endIndex); //a
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppMarketModel)->totalCovariance ( _endIndex );   
        Cephei::QL::Math::CMatrix^ _nrv = REFNEW Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

